﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IdeaNMR.Shared;
using IdeaNMR.Web.Core;
using IdeaNMR.Shared.Utils;

namespace IdeaNMR.Web
{
    /// <summary>
    /// The page allows user to browse, add, modify and delete his/her samples.
    /// </summary>
    /// <remarks>
    /// The main content is located in a MultiView mvCategories
    /// that has 3 views
    /// <list type="">
    /// <item>
    /// To display all spectrometers in a GridView gvAllSamples
    /// </item>
    /// <item>
    /// To show a single sample in a form
    /// </item>
    /// <item>
    /// Provides a form to add or modify a sample
    /// </item>
    /// </list>
    /// The request parameter TargetID specifies which sample to display.
    /// When page is new, the first view is displayed if TargetID is not specified.
    /// Alternatively the second view is displayed with the sample with TargetID.
    /// See for detailes in <seealso cref="IdeaNMR.Web.MySpectrometers"/>
    /// </remarks>
    public partial class MySamples : IdeaNMR_BasePage
    {
        #region Page Lifecycle
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            this.Master.Title = "My Samples";
        }
        #endregion

        #region Event Handlers
        protected void SampTable_OnSelected(object sender, IdeaNMR.Web.ItemSelectedCommandEventArgs e)
        {
            UriBuilder uri = new UriBuilder(ScrewTurnWikiFacade.MainUrl);
            uri.Path += "Sample_New.ashx";
            uri.AddQueryParameter("TargetID", e.SelectedItem.Id.ToString());

            this.RedirectParent(uri.Uri);
        }

        protected void btAddSample_Click(object sender, EventArgs e)
        {
            UriBuilder uri = new UriBuilder(ScrewTurnWikiFacade.MainUrl);
            uri.Path += "Sample_New.ashx";

            this.RedirectParent(uri.Uri);
        }
     
        //protected void butSubmit_Command(object sender, CommandEventArgs e)
        //{
        //    if (!Page.IsValid) return;

        //    using (IdeaNMRdbContainer ideanmrData = new IdeaNMRdbContainer())
        //    {
        //        Sample addSamp = new Sample();
        //        addSamp.Datetime = DateTime.Now;
        //        addSamp.Id = Guid.NewGuid();

        //        addSamp.Name = tbName.Text;
        //        addSamp.Buffer = tbBuffer.Text;
        //        addSamp.Labeling = tbLabeling.Text;
        //        addSamp.Info = tbInfo.Text;
        //        addSamp.ContactPerson = tbContactPerson.Text;
        //        addSamp.ContactEmail = tbContactEmail.Text;
        //        addSamp.Script = tbScript.Text;

        //        switch (e.CommandName.ToString())
        //        {
        //            case "Add":
        //                addSamp.VersionInfo = "First Release.";
        //                addSamp.VersionNumber = 0;
        //                addSamp.FirstVersion = addSamp;
        //                break;
        //            case "Update":
        //                Guid upSamp;
        //                try
        //                {
        //                    upSamp = new Guid(e.CommandArgument.ToString());
        //                }
        //                catch
        //                {
        //                    SampTable.Refresh();
        //                    mvCategories.ActiveViewIndex = 0;
        //                    return;
        //                }
        //                addSamp.VersionInfo = tbVersionInfo.Text;
        //                addSamp.VersionNumber = (from samp in ideanmrData.BaseObjects.OfType<Sample>()
        //                                         where samp.User.Username == ScrewTurnWikiFacade.Username
        //                                         where samp.Id == upSamp
        //                                         select samp.FirstVersion.DerivedVersions.FirstOrDefault(x => x.VersionNumber ==
        //                                             samp.FirstVersion.DerivedVersions.Max(y => y.VersionNumber)).VersionNumber).First() + 1;
        //                addSamp.FirstVersion = (from samp in ideanmrData.BaseObjects.OfType<Sample>()
        //                                        where samp.User.Username == ScrewTurnWikiFacade.Username
        //                                        where samp.Id == upSamp
        //                                        select samp.FirstVersion).First();
        //                break;
        //            default:
        //                throw (new ArgumentException("Only Add or Update values are possible."));
        //        }

        //        User author = (from usr in ideanmrData.Users
        //                                        where usr.Username == ScrewTurnWikiFacade.Username
        //                                        select usr).First<User>();
        //        author.Samples.Add(addSamp);
        //        ideanmrData.SaveChanges();
        //    }
        //    SampTable.Refresh();
        //    mvCategories.ActiveViewIndex = 0;
        //}
        #endregion EventHandlers
    }
}